#pragma once

#include <uapi/cdefs.h>

/* VMCS */

/* 16-bit control fields */
#define VMCS_VPID 0x00000000
#define VMCS_PIR_VECTOR 0x00000002

/* 16-bit guest-state fields */
#define VMCS_GUEST_ES_SELECTOR 0x00000800
#define VMCS_GUEST_CS_SELECTOR 0x00000802
#define VMCS_GUEST_SS_SELECTOR 0x00000804
#define VMCS_GUEST_DS_SELECTOR 0x00000806
#define VMCS_GUEST_FS_SELECTOR 0x00000808
#define VMCS_GUEST_GS_SELECTOR 0x0000080a
#define VMCS_GUEST_LDTR_SELECTOR 0x0000080c
#define VMCS_GUEST_TR_SELECTOR 0x0000080e
#define VMCS_GUEST_INTR_STATUS 0x00000810
#define VMCS_GUEST_PML_INDEX 0x00000812

/* 16-bit host-state fields */
#define VMCS_HOST_ES_SELECTOR 0x00000c00
#define VMCS_HOST_CS_SELECTOR 0x00000c02
#define VMCS_HOST_SS_SELECTOR 0x00000c04
#define VMCS_HOST_DS_SELECTOR 0x00000c06
#define VMCS_HOST_FS_SELECTOR 0x00000c08
#define VMCS_HOST_GS_SELECTOR 0x00000c0a
#define VMCS_HOST_TR_SELECTOR 0x00000c0c

/* 64-bit control fields */
#define VMCS_IO_BITMAP_A 0x00002000
#define VMCS_IO_BITMAP_B 0x00002002
#define VMCS_MSR_BITMAP 0x00002004
#define VMCS_EXIT_MSR_STORE 0x00002006
#define VMCS_EXIT_MSR_LOAD 0x00002008
#define VMCS_ENTRY_MSR_LOAD 0x0000200A
#define VMCS_EXECUTIVE_VMCS 0x0000200C
#define VMCS_TSC_OFFSET 0x00002010
#define VMCS_VIRTUAL_APIC 0x00002012
#define VMCS_APIC_ACCESS 0x00002014
#define VMCS_PIR_DESC 0x00002016
#define VMCS_EPTP 0x0000201A
#define VMCS_EOI_EXIT0 0x0000201c
#define VMCS_EOI_EXIT1 0x0000201e
#define VMCS_EOI_EXIT2 0x00002020
#define VMCS_EOI_EXIT3 0x00002022
#define VMCS_EOI_EXIT(vector) (VMCS_EOI_EXIT0 + ((vector) / 64) * 2)

/* 64-bit guest-state fields */
#define VMCS_LINK_POINTER 0x00002800
#define VMCS_GUEST_IA32_DEBUGCTL 0x00002802
#define VMCS_GUEST_IA32_PAT 0X00002804
#define VMCS_GUEST_IA32_EFER 0x00002806
#define VMCS_GUEST_IA32_PERF_GLOBAL_CTRL 0x00002808
#define VMCS_GUEST_PDPTE0 0x0000280a
#define VMCS_GUEST_PDPTE1 0x0000280c
#define VMCS_GUEST_PDPTE2 0x0000280e
#define VMCS_GUEST_PDPTE3 0x00002810
#define VMCS_GUEST_IA32_BNDCFGS 0x00002812

/* 64-bit host-state fields */
#define VMCS_HOST_IA32_PAT 0x00002c00
#define VMCS_HOST_IA32_EFER 0x00002c02
#define VMCS_HOST_IA32_PERF_GLOBAL_CTRL 0x00002c04

/* 32-bit control fields */
#define VMCS_PINBASED_CTLS 0x00004000
#define VMCS_PRI_PROCBASED_CTLS 0x00004002
#define VMCS_EXCEPTION_BITMAP 0x00004004
#define VMCS_PF_ERROR_MASK 0x00004006
#define VMCS_PF_ERROR_MATCH 0x00004008
#define VMCS_CR3_TARGET_COUNT 0x0000400a
#define VMCS_EXIT_CTLS 0x0000400c
#define VMCS_EXIT_MSR_STORE_COUNT 0x0000400e
#define VMCS_EXIT_MSR_LOAD_COUNT 0x00004010
#define VMCS_ENTRY_CTLS 0x00004012
#define VMCS_ENTRY_MSR_LOAD_COUNT 0x00004014
#define VMCS_ENTRY_INTR_INFO 0x00004016
#define VMCS_ENTRY_EXCEPTION_ERROR 0x00004018
#define VMCS_ENTRY_INST_LENGTH 0x0000401a
#define VMCS_TPR_THRESHOLD 0x0000401c
#define VMCS_SEC_PROCBASED_CTLS 0x0000401e
#define VMCS_PLE_GAP 0x00004020
#define VMCS_PLE_WINDOW 0x00004022

/* 32-bit read-only data fields */
#define VMCS_INSTRUCTION_ERROR 0x00004400
#define VMCS_EXIT_REASON 0x00004402
#define VMCS_EXIT_INTR_INFO 0x00004404
#define VMCS_EXIT_INTR_ERRCODE 0x00004406
#define VMCS_IDT_VECTORING_INFO 0x00004408
#define VMCS_IDT_VECTORING_ERROR 0x0000440a
#define VMCS_EXIT_INSTRUCTION_LENGTH 0x0000440c
#define VMCS_EXIT_INSTRUCTION_INFO 0x0000440e

/* 32-bit guest-state fields */
#define VMCS_GUEST_ES_LIMIT 0x00004800
#define VMCS_GUEST_CS_LIMIT 0x00004802
#define VMCS_GUEST_SS_LIMIT 0x00004804
#define VMCS_GUEST_DS_LIMIT 0x00004806
#define VMCS_GUEST_FS_LIMIT 0x00004808
#define VMCS_GUEST_GS_LIMIT 0x0000480a
#define VMCS_GUEST_LDTR_LIMIT 0x0000480c
#define VMCS_GUEST_TR_LIMIT 0x0000480e
#define VMCS_GUEST_GDTR_LIMIT 0x00004810
#define VMCS_GUEST_IDTR_LIMIT 0x00004812
#define VMCS_GUEST_ES_AR_BYTES 0x00004814
#define VMCS_GUEST_CS_AR_BYTES 0x00004816
#define VMCS_GUEST_SS_AR_BYTES 0x00004818
#define VMCS_GUEST_DS_AR_BYTES 0x0000481a
#define VMCS_GUEST_FS_AR_BYTES 0x0000481c
#define VMCS_GUEST_GS_AR_BYTES 0x0000481e
#define VMCS_GUEST_LDTR_AR_BYTES 0x00004820
#define VMCS_GUEST_TR_AR_BYTES 0x00004822
#define VMCS_GUEST_INTERRUPTIBILITY_INFO 0x00004824
#define VMCS_GUEST_ACTIVITY_STATE 0X00004826
#define VMCS_GUEST_SYSENTER_CS 0x0000482a
#define VMCS_PREEMPTION_TIMER_VALUE 0x0000482e

/* 32-bit host-state field */
#define VMCS_HOST_IA32_SYSENTER_CS 0x00004c00

/* Natural Width control fields */
#define VMCS_CR0_MASK                   0x00006000
#define VMCS_CR4_MASK                   0x00006002
#define VMCS_CR0_SHADOW                 0x00006004
#define VMCS_CR4_SHADOW                 0x00006006
#define VMCS_CR3_TARGET0                0x00006008
#define VMCS_CR3_TARGET1                0x0000600A
#define VMCS_CR3_TARGET2                0x0000600C
#define VMCS_CR3_TARGET3                0x0000600E

/* Natural Width read-only fields */
#define VMCS_EXIT_QUALIFICATION         0x00006400
#define VMCS_IO_RCX                     0x00006402
#define VMCS_IO_RSI                     0x00006404
#define VMCS_IO_RDI                     0x00006406
#define VMCS_IO_RIP                     0x00006408
#define VMCS_GUEST_LINEAR_ADDRESS       0x0000640A

/* natural-width guest-state fields */
#define VMCS_GUEST_CR0 0x00006800
#define VMCS_GUEST_CR3 0x00006802
#define VMCS_GUEST_CR4 0x00006804
#define VMCS_GUEST_ES_BASE 0x00006806
#define VMCS_GUEST_CS_BASE 0x00006808
#define VMCS_GUEST_SS_BASE 0x0000680a
#define VMCS_GUEST_DS_BASE 0x0000680c
#define VMCS_GUEST_FS_BASE 0x0000680e
#define VMCS_GUEST_GS_BASE 0x00006810
#define VMCS_GUEST_LDTR_BASE 0x00006812
#define VMCS_GUEST_TR_BASE 0x00006814
#define VMCS_GUEST_GDTR_BASE 0x00006816
#define VMCS_GUEST_IDTR_BASE 0x00006818
#define VMCS_GUEST_DR7 0x0000681a
#define VMCS_GUEST_RSP 0x0000681c
#define VMCS_GUEST_RIP 0x0000681e
#define VMCS_GUEST_RFLAGS 0x00006820
#define VMCS_GUEST_PENDING_DBG_EXCEPTIONS 0x00006822
#define VMCS_GUEST_IA32_SYSENTER_ESP 0x00006824
#define VMCS_GUEST_IA32_SYSENTER_EIP 0x00006826

/* natural-width host-state fields */
#define VMCS_HOST_CR0 0x00006c00
#define VMCS_HOST_CR3 0x00006c02
#define VMCS_HOST_CR4 0x00006c04
#define VMCS_HOST_FS_BASE 0x00006c06
#define VMCS_HOST_GS_BASE 0x00006c08
#define VMCS_HOST_TR_BASE 0x00006c0a
#define VMCS_HOST_GDTR_BASE 0x00006c0c
#define VMCS_HOST_IDTR_BASE 0x00006c0e
#define VMCS_HOST_IA32_SYSENTER_ESP 0x00006c10
#define VMCS_HOST_IA32_SYSENTER_EIP 0x00006c12
#define VMCS_HOST_RSP 0x00006c14
#define VMCS_HOST_RIP 0x00006c16

/* Exit reasons */
#define EXIT_REASON_EXCEPTION           0
#define EXIT_REASON_EXT_INTR            1
#define EXIT_REASON_TRIPLE_FAULT        2
#define EXIT_REASON_INIT                3
#define EXIT_REASON_SIPI                4
#define EXIT_REASON_IO_SMI              5
#define EXIT_REASON_SMI                 6
#define EXIT_REASON_INTR_WINDOW         7
#define EXIT_REASON_NMI_WINDOW          8
#define EXIT_REASON_TASK_SWITCH         9
#define EXIT_REASON_CPUID               10
#define EXIT_REASON_GETSEC              11
#define EXIT_REASON_HLT                 12
#define EXIT_REASON_INVD                13
#define EXIT_REASON_INVLPG              14
#define EXIT_REASON_RDPMC               15
#define EXIT_REASON_RDTSC               16
#define EXIT_REASON_RSM                 17
#define EXIT_REASON_VMCALL              18
#define EXIT_REASON_VMCLEAR             19
#define EXIT_REASON_VMLAUNCH            20
#define EXIT_REASON_VMPTRLD             21
#define EXIT_REASON_VMPTRST             22
#define EXIT_REASON_VMREAD              23
#define EXIT_REASON_VMRESUME            24
#define EXIT_REASON_VMWRITE             25
#define EXIT_REASON_VMXOFF              26
#define EXIT_REASON_VMXON               27
#define EXIT_REASON_CR_ACCESS           28
#define EXIT_REASON_DR_ACCESS           29
#define EXIT_REASON_INOUT               30
#define EXIT_REASON_RDMSR               31
#define EXIT_REASON_WRMSR               32
#define EXIT_REASON_INVAL_VMCS          33
#define EXIT_REASON_INVAL_MSR           34
#define EXIT_REASON_MWAIT               36
#define EXIT_REASON_MTF                 37
#define EXIT_REASON_MONITOR             39
#define EXIT_REASON_PAUSE               40
#define EXIT_REASON_MCE_DURING_ENTRY    41
#define EXIT_REASON_TPR                 43
#define EXIT_REASON_APIC_ACCESS         44
#define EXIT_REASON_VIRTUALIZED_EOI     45
#define EXIT_REASON_GDTR_IDTR           46
#define EXIT_REASON_LDTR_TR             47
#define EXIT_REASON_EPT_FAULT           48
#define EXIT_REASON_EPT_MISCONFIG       49
#define EXIT_REASON_INVEPT              50
#define EXIT_REASON_RDTSCP              51
#define EXIT_REASON_VMX_PREEMPT         52
#define EXIT_REASON_INVVPID             53
#define EXIT_REASON_WBINVD              54
#define EXIT_REASON_XSETBV              55
#define EXIT_REASON_APIC_WRITE          56

/* VMCS interrupt information fields */
#define VMCS_INTR_VALID BIT32(31)
#define VMCS_INTR_V_MASK BITS32(7, 0)
#define VMCS_INTR_T_MASK BITS32(10, 8) /* Interruption-info type */
#define VMCS_INTR_T_SHIFT 8
#define VMCS_INTR_T_HWINTR (0 << VMCS_INTR_T_SHIFT)
#define VMCS_INTR_T_NMI (2 << VMCS_INTR_T_SHIFT)
#define VMCS_INTR_T_HWEXCEPTION (3 << VMCS_INTR_T_SHIFT)
#define VMCS_INTR_T_SWINTR (4 << VMCS_INTR_T_SHIFT)
#define VMCS_INTR_T_PRIV_SWEXCEPTION (5 << VMCS_INTR_T_SHIFT)
#define VMCS_INTR_T_SWEXCEPTION (6 << VMCS_INTR_T_SHIFT)
#define VMCS_INTR_DEL_ERRCODE BIT32(11)

#define VMX_MISC_TIMER_RESOLUTION BITS32(4, 0)

#ifndef __ASSEMBLER__

struct vmcs {
    uint32_t revision_id : 31;
    uint32_t shadow : 1;
    uint32_t abort;
    char data[];
} __packed;

typedef uint16_t vpid_t;

#define INVVPID_INDIVIDUAL_ADDRESS            0x00ull
#define INVVPID_SINGLE_CONTEXT                0x01ull
#define INVVPID_ALL_CONTEXTS                  0x02ull
#define INVVPID_SINGLE_CONTEXT_RETAIN_GLOBALS 0x03ull

static inline void invvpid(uint64_t mode, uintptr_t linaddr, uint16_t vpid)
{
    __uint128_t desc = ((__uint128_t) linaddr) << 64 | vpid;
    asm volatile ("invvpid (%0), %1" : : "r" (&desc), "r" (mode) : "memory");
}

static inline vpid_t pid_vpid(pid_t pid)
{
    _Static_assert(NPROC <= UINT16_MAX, "NPROC must fit in intel VPID");
    assert(pid != 0, "VPID cannot be zero");
    return (vpid_t) pid;
}

#endif /* !__ASSEMBLER__ */
